<script setup>
import {
  listPatient,
  updatePatient,
  addPatient,
  delPatient,
  getPatientInfo,
} from "@/api/hospital";
const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict("sys_user_sex");
import { parseTime } from "@/utils/ruoyi";
const loading = ref(true);
const list = ref([]);
const open = ref(false);
const title = ref("");
const total = ref(0);

// 查询参数
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    name: undefined,
    phone: undefined,
    identity_code: undefined,
    outpatient_code: undefined,
    inpatient_code: undefined,
  },
  rules: {
    name: [
      { required: true, message: "患者姓名不能为空", trigger: "blur" },
      {
        min: 2,
        max: 20,
        message: "患者姓名长度必须介于 2 和 20 之间",
        trigger: "blur",
      },
    ],
    gender: [
      {
        required: true,
        message: "必须指定性别",
        trigger: "blur",
      },
    ],
    identity_code: [
      {
        required: true,
        message: "身份证号不能为空",
        trigger: "blur",
      },
      {
        trigger: "blur",
        min: 1,
        max: 18,
        message: "身份证号长度必须是18位数字",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.name = undefined;
  queryParams.value.phone = undefined;
  queryParams.value.identity_code = undefined;
  queryParams.value.outpatient_code = undefined;
  queryParams.value.inpatient_code = undefined;
  handleQuery();
}
/** 查询列表 */
function getList() {
  loading.value = true;
  listPatient(queryParams.value).then((res) => {
    loading.value = false;
    list.value = res.data;
    total.value = res.total;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增患者信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const patiendId = row.id;
  getPatientInfo(patiendId).then((response) => {
    form.value = response.data;
    if (form.value.birthday) {
      form.value.birthday = parseTime(form.value.birthday, "{y}-{m}-{d}");
    }
    open.value = true;
    title.value = "修改患者信息";
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const patiendName = row.name || names.value;
  const patiendId = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除患者姓名为"' + patiendName + '"的数据项？')
    .then(function () {
      return delPatient(patiendId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["patientRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePatient(form.value).then((response) => {
          if (response.code == 200) proxy.$modal.msgSuccess("修改患者成功");
          else proxy.$modal.alertError(response.msg);
          open.value = false;
          getList();
        });
      } else {
        addPatient(form.value).then((response) => {
          if (response.code == 200) proxy.$modal.msgSuccess("新增成功");
          else proxy.$modal.alertError(response.msg);
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 重置操作表单 */
function reset() {
  form.value = {
    id: undefined,
  };
  proxy.resetForm("patientRef");
}

getList();
</script>

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="identity_code">
        <el-input
          v-model="queryParams.identity_code"
          placeholder="请输入身份证号码"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门诊号" prop="outpatient_code">
        <el-input
          v-model="queryParams.outpatient_code"
          placeholder="请输入门诊号"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住院号" prop="inpatient_code">
        <el-input
          v-model="queryParams.inpatient_code"
          placeholder="请输入住院号"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column
        label="患者编号"
        align="center"
        key="id"
        prop="id"
        width="100"
        sortable
      />
      <el-table-column label="姓名" align="center" key="name" prop="name" />
      <el-table-column
        label="性别"
        align="center"
        key="genderStr"
        prop="genderStr"
      />
      <el-table-column
        label="身份证号码"
        align="center"
        key="identity_code"
        prop="identity_code"
      />
      <el-table-column
        label="手机号码"
        align="center"
        key="phone"
        prop="phone"
      />
      <el-table-column
        label="门诊号"
        align="center"
        key="outpatient_code"
        prop="outpatient_code"
      />
      <el-table-column
        label="住院号"
        align="center"
        key="inpatient_code"
        prop="inpatient_code"
      />
      <el-table-column label="操作" align="center" width="100">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改患者信息配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="patientRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="患者姓名" prop="name">
              <el-input
                v-model="form.name"
                placeholder="患者姓名"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select
                filterable
                v-model="form.gender"
                placeholder="请选择性别"
              >
                <el-option
                  v-for="dict in sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入联系电话"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker
                v-model="form.birthday"
                type="date"
                placeholder="请输入出生日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="身份证" prop="identity_code">
              <el-input
                v-model="form.identity_code"
                placeholder="请输入身份证号码"
                maxlength="18"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="住址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入住址"
                maxlength="50"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="门诊号" prop="outpatient_code">
              <el-input
                v-model="form.outpatient_code"
                placeholder="请输入门诊号"
                maxlength="20"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住院号" prop="inpatient_code">
              <el-input
                v-model="form.inpatient_code"
                placeholder="请输入住院号"
                maxlength="20"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped></style>
